<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class OwnerInfoModel extends AbstractModel
{
    protected $tableName = 't_owner_info';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getOwner($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('业主信息表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colVarChar('uid', 25)->setDefaultValue('')->setIsNotNull()->setColumnComment('用户id');
        $table->colVarChar('project', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('项目名称');
        $table->colVarChar('region', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('组团区域');
        $table->colInt('build', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('楼栋号');
        $table->colInt('unit', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('单元号');
        $table->colInt('floor', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('楼层');
        $table->colInt('house', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('房间号');
        $table->colVarChar('name', 99)->setDefaultValue('')->setIsNotNull()->setColumnComment('业主姓名');
        $table->colVarChar('tel', 99)->setDefaultValue('')->setIsNotNull()->setColumnComment('联系电话');
        $table->colVarChar('id_card', 99)->setDefaultValue('')->setIsNotNull()->setColumnComment('身份证号');
        $table->colVarChar('area_house', 99)->setDefaultValue('')->setIsNotNull()->setColumnComment('房屋面积');
        $table->colVarChar('area_build', 99)->setDefaultValue('')->setIsNotNull()->setColumnComment('建筑面积');
        $table->colVarChar('address', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('居住地址');
        $table->colVarChar('memo', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('备注');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('uid_index', 'uid');
        $table->indexNormal('name_index', 'name');
        $table->indexNormal('tel_index', 'tel');
        $table->indexNormal('id_card_index', 'id_card');

        return $table;
    }
}